<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>容错示例 | AKKA 2.3.6 Scala 文档</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../chapter3/04_dispatchers.html" />
    
    
    <link rel="prev" href="../chapter3/03_fault_tolerance.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="3.3.1" data-basepath=".." data-revision="1442802141200">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="chapter1/introduction.html">
            
                
                    <a href="../chapter1/introduction.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         引言
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.1" data-path="chapter1/01_what_is_akka.html">
            
                
                    <a href="../chapter1/01_what_is_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         Akka是什么?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="chapter1/02_why_akka.html">
            
                
                    <a href="../chapter1/02_why_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         为什么使用Akka?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="chapter1/03_getting_started.html">
            
                
                    <a href="../chapter1/03_getting_started.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         入门
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.4" data-path="chapter1/04_the_obligatory_hello_world.html">
            
                
                    <a href="../chapter1/04_the_obligatory_hello_world.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                         必修的“Hello World”
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.5" data-path="chapter1/05_usecase_and_deployment_scenarios.html">
            
                
                    <a href="../chapter1/05_usecase_and_deployment_scenarios.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                         用例和部署场景
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.6" data-path="chapter1/06_examples_of_usecases_for_akka.html">
            
                
                    <a href="../chapter1/06_examples_of_usecases_for_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                         Akka使用实例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="chapter2/general.html">
            
                
                    <a href="../chapter2/general.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         概述
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="2.1" data-path="chapter2/01_terminology_concepts.html">
            
                
                    <a href="../chapter2/01_terminology_concepts.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                         术语，概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.2" data-path="chapter2/02_actor_systems.html">
            
                
                    <a href="../chapter2/02_actor_systems.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                         Actor系统
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.3" data-path="chapter2/03_what_is_an_actor.html">
            
                
                    <a href="../chapter2/03_what_is_an_actor.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                         什么是Actor?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.4" data-path="chapter2/04_supervision_and_monitoring.html">
            
                
                    <a href="../chapter2/04_supervision_and_monitoring.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                         监管与监控
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.5" data-path="chapter2/05_actor_references_paths_and_addresses.html">
            
                
                    <a href="../chapter2/05_actor_references_paths_and_addresses.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                         Actor引用, 路径与地址
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.6" data-path="chapter2/06_location_transparency.html">
            
                
                    <a href="../chapter2/06_location_transparency.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                         位置透明性
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.7" data-path="chapter2/07_akka_and_the_java_memory_model.html">
            
                
                    <a href="../chapter2/07_akka_and_the_java_memory_model.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                         Akka与Java内存模型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.8" data-path="chapter2/08_message_delivery_reliability.html">
            
                
                    <a href="../chapter2/08_message_delivery_reliability.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                         消息发送语义
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.9" data-path="chapter2/09_configuration.html">
            
                
                    <a href="../chapter2/09_configuration.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                         配置
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="chapter3/actors.html">
            
                
                    <a href="../chapter3/actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         Actors
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.1" data-path="chapter3/01_actors.html">
            
                
                    <a href="../chapter3/01_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                         Actors
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.2" data-path="chapter3/02_typed_actors.html">
            
                
                    <a href="../chapter3/02_typed_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                         有类型Actor
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.3" data-path="chapter3/03_fault_tolerance.html">
            
                
                    <a href="../chapter3/03_fault_tolerance.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                         容错
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter active" data-level="3.3.1" data-path="chapter3/03-1_fault_tolerance_sample.html">
            
                
                    <a href="../chapter3/03-1_fault_tolerance_sample.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                         容错示例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3.4" data-path="chapter3/04_dispatchers.html">
            
                
                    <a href="../chapter3/04_dispatchers.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                         调度器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.5" data-path="chapter3/05_mailboxes.html">
            
                
                    <a href="../chapter3/05_mailboxes.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                         邮箱
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.6" data-path="chapter3/06_routing.html">
            
                
                    <a href="../chapter3/06_routing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                         路由
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.7" data-path="chapter3/07_fsm.html">
            
                
                    <a href="../chapter3/07_fsm.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                         有限状态机(FSM)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.8" data-path="chapter3/08_persistence.html">
            
                
                    <a href="../chapter3/08_persistence.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                         持久化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.9" data-path="chapter3/09_testing_actor_systems.html">
            
                
                    <a href="../chapter3/09_testing_actor_systems.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                         测试Actor系统
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.9.1" data-path="chapter3/09_1_testkit-example.html">
            
                
                    <a href="../chapter3/09_1_testkit-example.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.1.</b>
                        
                         TestKit实例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3.10" data-path="chapter3/10_actor_dsl.html">
            
                
                    <a href="../chapter3/10_actor_dsl.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                         Actor DSL
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="chapter4/futures_and_agents.html">
            
                
                    <a href="../chapter4/futures_and_agents.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         Futures与Agents
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="4.1" data-path="chapter4/01_futures.html">
            
                
                    <a href="../chapter4/01_futures.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                         Futures
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.2" data-path="chapter4/02_agents.html">
            
                
                    <a href="../chapter4/02_agents.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                         Agents
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="chapter5/networking.html">
            
                
                    <a href="../chapter5/networking.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         网络
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="5.1" data-path="chapter5/01_cluster_cpecification.html">
            
                
                    <a href="../chapter5/01_cluster_cpecification.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                         集群规格
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.2" data-path="chapter5/02_cluster_usage.html">
            
                
                    <a href="../chapter5/02_cluster_usage.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                         集群用法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.3" data-path="chapter5/03_remoting.html">
            
                
                    <a href="../chapter5/03_remoting.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                         远程
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.4" data-path="chapter5/04_serialization.html">
            
                
                    <a href="../chapter5/04_serialization.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                         序列化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.5" data-path="chapter5/05_io.html">
            
                
                    <a href="../chapter5/05_io.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                         I/O
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.6" data-path="chapter5/06_using_tcp.html">
            
                
                    <a href="../chapter5/06_using_tcp.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                         使用TCP
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.7" data-path="chapter5/07_using_udp.html">
            
                
                    <a href="../chapter5/07_using_udp.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                         使用UDP
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.8" data-path="chapter5/08_zeromq.html">
            
                
                    <a href="../chapter5/08_zeromq.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                         ZeroMQ
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.9" data-path="chapter5/09_camel.html">
            
                
                    <a href="../chapter5/09_camel.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                         Camel
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="chapter6/utilities.html">
            
                
                    <a href="../chapter6/utilities.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         实用工具
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="6.1" data-path="chapter6/01_event_bus.html">
            
                
                    <a href="../chapter6/01_event_bus.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                         事件总线
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.2" data-path="chapter6/02_logging.html">
            
                
                    <a href="../chapter6/02_logging.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                         日志
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.3" data-path="chapter6/03_scheduler.html">
            
                
                    <a href="../chapter6/03_scheduler.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                         调度器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.4" data-path="chapter6/04_duration.html">
            
                
                    <a href="../chapter6/04_duration.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                         Duration
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.5" data-path="chapter6/05_circuit_breaker.html">
            
                
                    <a href="../chapter6/05_circuit_breaker.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                         线路断路器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.6" data-path="chapter6/06_akka_extensions.html">
            
                
                    <a href="../chapter6/06_akka_extensions.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                         Akka扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.7" data-path="chapter6/07_microkernel.html">
            
                
                    <a href="../chapter6/07_microkernel.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                         微内核
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="chapter7/howto_common_patterns.html">
            
                
                    <a href="../chapter7/howto_common_patterns.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         如何使用：常用模式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="chapter8/experimental_modules.html">
            
                
                    <a href="../chapter8/experimental_modules.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         实验模块
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="8.1" data-path="chapter3/08_persistence.html">
            
                
                    <a href="../chapter3/08_persistence.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                         持久化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.2" data-path="chapter8/02_multi_node_testing.html">
            
                
                    <a href="../chapter8/02_multi_node_testing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                         多节点测试
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.3" data-path="chapter8/03_actors.html">
            
                
                    <a href="../chapter8/03_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                         Actors(使用Java的Lambda支持)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.4" data-path="chapter8/04_fsm.html">
            
                
                    <a href="../chapter8/04_fsm.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                         FSM(使用Java的Lambda支持)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.5" data-path="chapter8/05_external_contributions.html">
            
                
                    <a href="../chapter8/05_external_contributions.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                         外部贡献
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="chapter9/information_for_akka_developers.html">
            
                
                    <a href="../chapter9/information_for_akka_developers.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         Akka开发者信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="9.1" data-path="chapter9/01_building_akka.html">
            
                
                    <a href="../chapter9/01_building_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                         构建Akka
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.2" data-path="chapter9/02_multi_jvm_testing.html">
            
                
                    <a href="../chapter9/02_multi_jvm_testing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                         多JVM测试
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.3" data-path="chapter9/03_io_layer_design.html">
            
                
                    <a href="../chapter9/03_io_layer_design.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                         I/O层设计
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.4" data-path="chapter9/04_developer_guidelines.html">
            
                
                    <a href="../chapter9/04_developer_guidelines.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                         开发指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.5" data-path="chapter9/05_documentation_guidelines.html">
            
                
                    <a href="../chapter9/05_documentation_guidelines.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                         文档指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.6" data-path="chapter9/06_team.html">
            
                
                    <a href="../chapter9/06_team.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.6.</b>
                        
                         团队
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="chapter10/project_information.html">
            
                
                    <a href="../chapter10/project_information.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         工程信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="10.1" data-path="chapter10/01_migration_guides.html">
            
                
                    <a href="../chapter10/01_migration_guides.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                         迁移指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.2" data-path="chapter10/02_issue_tracking.html">
            
                
                    <a href="../chapter10/02_issue_tracking.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                         问题追踪
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.3" data-path="chapter10/03_licenses.html">
            
                
                    <a href="../chapter10/03_licenses.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                         许可证
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.4" data-path="chapter10/04_sponsors.html">
            
                
                    <a href="../chapter10/04_sponsors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                         赞助商
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.5" data-path="chapter10/05_project.html">
            
                
                    <a href="../chapter10/05_project.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                         项目
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="chapter11/additional_information.html">
            
                
                    <a href="../chapter11/additional_information.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         附加信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="11.1" data-path="chapter11/01_faq.html">
            
                
                    <a href="../chapter11/01_faq.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.1.</b>
                        
                         常见问题
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.2" data-path="chapter11/02_books.html">
            
                
                    <a href="../chapter11/02_books.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.2.</b>
                        
                         图书
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.3" data-path="chapter11/03_other_language_bindings.html">
            
                
                    <a href="../chapter11/03_other_language_bindings.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.3.</b>
                        
                         其他语言绑定
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.4" data-path="chapter11/04_akka_in_osgi.html">
            
                
                    <a href="../chapter11/04_akka_in_osgi.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.4.</b>
                        
                         Akka与OSGi
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.5" data-path="chapter11/05_incomplete_list_of_http_frameworks.html">
            
                
                    <a href="../chapter11/05_incomplete_list_of_http_frameworks.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.5.</b>
                        
                         部分HTTP框架名单
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >AKKA 2.3.6 Scala 文档</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_81">
                    
                        <h1 id="">容错示例</h1>
<p><img src="../images/faulttolerancesample-normal-flow.png" alt=""></p>
<p><em>以上图示了正常的消息流。</em></p>
<p><strong>正常流程：</strong></p>
<table>
<thead>
<tr>
<th>步骤</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td style="text-align:center">过程<code>Listener</code>启动工作。</td>
</tr>
<tr>
<td>2</td>
<td style="text-align:center"><code>Worker</code>通过定期向自己发送<code>Do</code>消息来计划工作</td>
</tr>
<tr>
<td>3, 4, 5</td>
<td style="text-align:center">接收到<code>Do</code>时<code>Worker</code>通知<code>CounterService</code>更新计数器值，三次。<code>Increment</code>消息被转发给<code>Counter</code>，它会更新自己的计数器变量并将当前值发给<code>Storage</code>。</td>
</tr>
<tr>
<td>6, 7</td>
<td style="text-align:center"><code>Worker</code>向<code>CounterService</code>请求计数器的当前值并将结果回送给<code>Listener</code>。</td>
</tr>
</tbody>
</table>
<p><img src="../images/faulttolerancesample-failure-flow.png" alt=""></p>
<p><em>以上图示了当发生数据库失败时的过程</em></p>
<p><strong>失败流程：</strong></p>
<table>
<thead>
<tr>
<th>步骤</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td style="text-align:center"><code>Storage</code>抛出 <code>StorageException</code>。</td>
</tr>
<tr>
<td>2</td>
<td style="text-align:center"><code>CounterService</code>是<code>Storage</code>的监管者，<code>StorageException</code>被抛出时它将重启<code>Storage</code>。</td>
</tr>
<tr>
<td>3, 4, 5, 6</td>
<td style="text-align:center"><code>Storage</code>仍旧失败，又被重启.</td>
</tr>
<tr>
<td>7</td>
<td style="text-align:center">在5秒内三次失败和重启后<code>Storage</code>被它的监管者，即<code>CounterService</code>终止。</td>
</tr>
<tr>
<td>8</td>
<td style="text-align:center"><code>CounterService</code>同时监视着<code>Storage</code>并在<code>Storage</code>被终止时收到<code>Terminated</code>消息...</td>
</tr>
<tr>
<td>9, 10, 11</td>
<td style="text-align:center">告诉<code>Counter</code>当前没有可用的<code>Storage</code>。</td>
</tr>
<tr>
<td>12</td>
<td style="text-align:center"><code>CounterService</code>计划一个<code>Reconnect</code>消息发给自己。</td>
</tr>
<tr>
<td>13, 14</td>
<td style="text-align:center">收到<code>Reconnect</code>消息后它创建一个新的<code>Storage</code>...</td>
</tr>
<tr>
<td>15, 16</td>
<td style="text-align:center">并通知<code>Counter</code>使用新的<code>Storage</code>。</td>
</tr>
</tbody>
</table>
<h5 id="a-namefull-source-code-of-the-fault-tolerance-samplea"><a name="full-source-code-of-the-fault-tolerance-sample"></a>容错示例完整源代码</h5>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> akka.actor._
<span class="hljs-keyword">import</span> akka.actor.<span class="hljs-type">SupervisorStrategy</span>._
<span class="hljs-keyword">import</span> scala.concurrent.duration._
<span class="hljs-keyword">import</span> akka.util.<span class="hljs-type">Timeout</span>
<span class="hljs-keyword">import</span> akka.event.<span class="hljs-type">LoggingReceive</span>
<span class="hljs-keyword">import</span> akka.pattern.{ ask, pipe }
<span class="hljs-keyword">import</span> com.typesafe.config.<span class="hljs-type">ConfigFactory</span>

<span class="hljs-comment">/**
 * Runs the sample
 */</span>
<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">FaultHandlingDocSample</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">App</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">Worker</span>._

  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">config</span> =</span> <span class="hljs-type">ConfigFactory</span>.parseString(<span class="hljs-string">"""
    akka.loglevel = "DEBUG"
    akka.actor.debug {
      receive = on
      lifecycle = on
    }
    """</span>)

  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">system</span> =</span> <span class="hljs-type">ActorSystem</span>(<span class="hljs-string">"FaultToleranceSample"</span>, config)
  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">worker</span> =</span> system.actorOf(<span class="hljs-type">Props</span>[<span class="hljs-type">Worker</span>], name = <span class="hljs-string">"worker"</span>)
  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">listener</span> =</span> system.actorOf(<span class="hljs-type">Props</span>[<span class="hljs-type">Listener</span>], name = <span class="hljs-string">"listener"</span>)
  <span class="hljs-comment">// start the work and listen on progress</span>
  <span class="hljs-comment">// note that the listener is used as sender of the tell,</span>
  <span class="hljs-comment">// i.e. it will receive replies from the worker</span>
  worker.tell(<span class="hljs-type">Start</span>, sender = listener)
}

<span class="hljs-comment">/**
 * Listens on progress from the worker and shuts down the system when enough
 * work has been done.
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Listener</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Actor</span> <span class="hljs-keyword"><span class="hljs-keyword">with</span></span> <span class="hljs-title">ActorLogging</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">Worker</span>._
  <span class="hljs-comment">// If we don't get any progress within 15 seconds then the service is unavailable</span>
  context.setReceiveTimeout(<span class="hljs-number">15</span> seconds)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> =</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Progress</span>(percent) =&gt;
      log.info(<span class="hljs-string">"Current progress: {} %"</span>, percent)
      <span class="hljs-keyword">if</span> (percent &gt;= <span class="hljs-number">100.0</span>) {
        log.info(<span class="hljs-string">"That's all, shutting down"</span>)
        context.system.shutdown()
      }

    <span class="hljs-keyword">case</span> <span class="hljs-type">ReceiveTimeout</span> =&gt;
      <span class="hljs-comment">// No progress within 15 seconds, ServiceUnavailable</span>
      log.error(<span class="hljs-string">"Shutting down due to unavailable service"</span>)
      context.system.shutdown()
  }
}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Worker</span> {</span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Start</span>
</span>  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Do</span>
</span>  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Progress</span>(</span>percent: <span class="hljs-type">Double</span>)
}

<span class="hljs-comment">/**
 * Worker performs some work when it receives the `Start` message.
 * It will continuously notify the sender of the `Start` message
 * of current ``Progress``. The `Worker` supervise the `CounterService`.
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Worker</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Actor</span> <span class="hljs-keyword"><span class="hljs-keyword">with</span></span> <span class="hljs-title">ActorLogging</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">Worker</span>._
  <span class="hljs-keyword">import</span> <span class="hljs-type">CounterService</span>._
  <span class="hljs-keyword">implicit</span> <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">askTimeout</span> =</span> <span class="hljs-type">Timeout</span>(<span class="hljs-number">5</span> seconds)

  <span class="hljs-comment">// Stop the CounterService child if it throws ServiceUnavailable</span>
  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">supervisorStrategy</span> =</span> <span class="hljs-type">OneForOneStrategy</span>() {
    <span class="hljs-keyword">case</span> _: <span class="hljs-type">CounterService</span>.<span class="hljs-type">ServiceUnavailable</span> =&gt; <span class="hljs-type">Stop</span>
  }

  <span class="hljs-comment">// The sender of the initial Start message will continuously be notified</span>
  <span class="hljs-comment">// about progress</span>
  <span class="hljs-keyword">var</span> progressListener: <span class="hljs-type">Option</span>[<span class="hljs-type">ActorRef</span>] = <span class="hljs-type">None</span>
  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">counterService</span> =</span> context.actorOf(<span class="hljs-type">Props</span>[<span class="hljs-type">CounterService</span>], name = <span class="hljs-string">"counter"</span>)
  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">totalCount</span> =</span> <span class="hljs-number">51</span>
  <span class="hljs-keyword">import</span> context.dispatcher <span class="hljs-comment">// Use this Actors' Dispatcher as ExecutionContext</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> =</span> <span class="hljs-type">LoggingReceive</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Start</span> <span class="hljs-keyword">if</span> progressListener.isEmpty =&gt;
      progressListener = <span class="hljs-type">Some</span>(sender)
      context.system.scheduler.schedule(<span class="hljs-type">Duration</span>.<span class="hljs-type">Zero</span>, <span class="hljs-number">1</span> second, self, <span class="hljs-type">Do</span>)

    <span class="hljs-keyword">case</span> <span class="hljs-type">Do</span> =&gt;
      counterService ! <span class="hljs-type">Increment</span>(<span class="hljs-number">1</span>)
      counterService ! <span class="hljs-type">Increment</span>(<span class="hljs-number">1</span>)
      counterService ! <span class="hljs-type">Increment</span>(<span class="hljs-number">1</span>)

      <span class="hljs-comment">// Send current progress to the initial sender</span>
      counterService ? <span class="hljs-type">GetCurrentCount</span> map {
        <span class="hljs-keyword">case</span> <span class="hljs-type">CurrentCount</span>(_, count) =&gt; <span class="hljs-type">Progress</span>(<span class="hljs-number">100.0</span> * count / totalCount)
      } pipeTo progressListener.get
  }
}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">CounterService</span> {</span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Increment</span>(</span>n: <span class="hljs-type">Int</span>)
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">GetCurrentCount</span>
</span>  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CurrentCount</span>(</span>key: <span class="hljs-type">String</span>, count: <span class="hljs-type">Long</span>)
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServiceUnavailable</span>(</span>msg: <span class="hljs-type">String</span>) <span class="hljs-keyword">extends</span> <span class="hljs-type">RuntimeException</span>(msg)

  <span class="hljs-keyword">private</span> <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Reconnect</span>
</span>}

<span class="hljs-comment">/**
 * Adds the value received in `Increment` message to a persistent
 * counter. Replies with `CurrentCount` when it is asked for `CurrentCount`.
 * `CounterService` supervise `Storage` and `Counter`.
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CounterService</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Actor</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">CounterService</span>._
  <span class="hljs-keyword">import</span> <span class="hljs-type">Counter</span>._
  <span class="hljs-keyword">import</span> <span class="hljs-type">Storage</span>._

  <span class="hljs-comment">// Restart the storage child when StorageException is thrown.</span>
  <span class="hljs-comment">// After 3 restarts within 5 seconds it will be stopped.</span>
  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">supervisorStrategy</span> =</span> <span class="hljs-type">OneForOneStrategy</span>(maxNrOfRetries = <span class="hljs-number">3</span>,
    withinTimeRange = <span class="hljs-number">5</span> seconds) {
      <span class="hljs-keyword">case</span> _: <span class="hljs-type">Storage</span>.<span class="hljs-type">StorageException</span> =&gt; <span class="hljs-type">Restart</span>
    }

  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">key</span> =</span> self.path.name
  <span class="hljs-keyword">var</span> storage: <span class="hljs-type">Option</span>[<span class="hljs-type">ActorRef</span>] = <span class="hljs-type">None</span>
  <span class="hljs-keyword">var</span> counter: <span class="hljs-type">Option</span>[<span class="hljs-type">ActorRef</span>] = <span class="hljs-type">None</span>
  <span class="hljs-keyword">var</span> backlog = <span class="hljs-type">IndexedSeq</span>.empty[(<span class="hljs-type">ActorRef</span>, <span class="hljs-type">Any</span>)]
  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">MaxBacklog</span> =</span> <span class="hljs-number">10000</span>

  <span class="hljs-keyword">import</span> context.dispatcher <span class="hljs-comment">// Use this Actors' Dispatcher as ExecutionContext</span>

  <span class="hljs-keyword">override</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">preStart</span>(</span>) {
    initStorage()
  }

  <span class="hljs-comment">/**
   * The child storage is restarted in case of failure, but after 3 restarts,
   * and still failing it will be stopped. Better to back-off than continuously
   * failing. When it has been stopped we will schedule a Reconnect after a delay.
   * Watch the child so we receive Terminated message when it has been terminated.
   */</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initStorage</span>(</span>) {
    storage = <span class="hljs-type">Some</span>(context.watch(context.actorOf(<span class="hljs-type">Props</span>[<span class="hljs-type">Storage</span>], name = <span class="hljs-string">"storage"</span>)))
    <span class="hljs-comment">// Tell the counter, if any, to use the new storage</span>
    counter foreach { _ ! <span class="hljs-type">UseStorage</span>(storage) }
    <span class="hljs-comment">// We need the initial value to be able to operate</span>
    storage.get ! <span class="hljs-type">Get</span>(key)
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> =</span> <span class="hljs-type">LoggingReceive</span> {

    <span class="hljs-keyword">case</span> <span class="hljs-type">Entry</span>(k, v) <span class="hljs-keyword">if</span> k == key &amp;&amp; counter == <span class="hljs-type">None</span> =&gt;
      <span class="hljs-comment">// Reply from Storage of the initial value, now we can create the Counter</span>
      <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">c</span> =</span> context.actorOf(<span class="hljs-type">Props</span>(classOf[<span class="hljs-type">Counter</span>], key, v))
      counter = <span class="hljs-type">Some</span>(c)
      <span class="hljs-comment">// Tell the counter to use current storage</span>
      c ! <span class="hljs-type">UseStorage</span>(storage)
      <span class="hljs-comment">// and send the buffered backlog to the counter</span>
      <span class="hljs-keyword">for</span> ((replyTo, msg) &lt;- backlog) c.tell(msg, sender = replyTo)
      backlog = <span class="hljs-type">IndexedSeq</span>.empty

    <span class="hljs-keyword">case</span> msg @ <span class="hljs-type">Increment</span>(n)    =&gt; forwardOrPlaceInBacklog(msg)

    <span class="hljs-keyword">case</span> msg @ <span class="hljs-type">GetCurrentCount</span> =&gt; forwardOrPlaceInBacklog(msg)

    <span class="hljs-keyword">case</span> <span class="hljs-type">Terminated</span>(actorRef) <span class="hljs-keyword">if</span> <span class="hljs-type">Some</span>(actorRef) == storage =&gt;
      <span class="hljs-comment">// After 3 restarts the storage child is stopped.</span>
      <span class="hljs-comment">// We receive Terminated because we watch the child, see initStorage.</span>
      storage = <span class="hljs-type">None</span>
      <span class="hljs-comment">// Tell the counter that there is no storage for the moment</span>
      counter foreach { _ ! <span class="hljs-type">UseStorage</span>(<span class="hljs-type">None</span>) }
      <span class="hljs-comment">// Try to re-establish storage after while</span>
      context.system.scheduler.scheduleOnce(<span class="hljs-number">10</span> seconds, self, <span class="hljs-type">Reconnect</span>)

    <span class="hljs-keyword">case</span> <span class="hljs-type">Reconnect</span> =&gt;
      <span class="hljs-comment">// Re-establish storage after the scheduled delay</span>
      initStorage()
  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">forwardOrPlaceInBacklog</span>(</span>msg: <span class="hljs-type">Any</span>) {
    <span class="hljs-comment">// We need the initial value from storage before we can start delegate to</span>
    <span class="hljs-comment">// the counter. Before that we place the messages in a backlog, to be sent</span>
    <span class="hljs-comment">// to the counter when it is initialized.</span>
    counter <span class="hljs-keyword">match</span> {
      <span class="hljs-keyword">case</span> <span class="hljs-type">Some</span>(c) =&gt; c forward msg
      <span class="hljs-keyword">case</span> <span class="hljs-type">None</span> =&gt;
        <span class="hljs-keyword">if</span> (backlog.size &gt;= <span class="hljs-type">MaxBacklog</span>)
          <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-type">ServiceUnavailable</span>(
            <span class="hljs-string">"CounterService not available, lack of initial value"</span>)
        backlog :+= (sender() -&gt; msg)
    }
  }

}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Counter</span> {</span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UseStorage</span>(</span>storage: <span class="hljs-type">Option</span>[<span class="hljs-type">ActorRef</span>])
}

<span class="hljs-comment">/**
 * The in memory count variable that will send current
 * value to the `Storage`, if there is any storage
 * available at the moment.
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>(</span>key: <span class="hljs-type">String</span>, initialValue: <span class="hljs-type">Long</span>) <span class="hljs-keyword">extends</span> <span class="hljs-type">Actor</span> {
  <span class="hljs-keyword">import</span> <span class="hljs-type">Counter</span>._
  <span class="hljs-keyword">import</span> <span class="hljs-type">CounterService</span>._
  <span class="hljs-keyword">import</span> <span class="hljs-type">Storage</span>._

  <span class="hljs-keyword">var</span> count = initialValue
  <span class="hljs-keyword">var</span> storage: <span class="hljs-type">Option</span>[<span class="hljs-type">ActorRef</span>] = <span class="hljs-type">None</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> =</span> <span class="hljs-type">LoggingReceive</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">UseStorage</span>(s) =&gt;
      storage = s
      storeCount()

    <span class="hljs-keyword">case</span> <span class="hljs-type">Increment</span>(n) =&gt;
      count += n
      storeCount()

    <span class="hljs-keyword">case</span> <span class="hljs-type">GetCurrentCount</span> =&gt;
      sender() ! <span class="hljs-type">CurrentCount</span>(key, count)

  }

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">storeCount</span>(</span>) {
    <span class="hljs-comment">// Delegate dangerous work, to protect our valuable state.</span>
    <span class="hljs-comment">// We can continue without storage.</span>
    storage foreach { _ ! <span class="hljs-type">Store</span>(<span class="hljs-type">Entry</span>(key, count)) }
  }

}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">Storage</span> {</span>
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Store</span>(</span>entry: <span class="hljs-type">Entry</span>)
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Get</span>(</span>key: <span class="hljs-type">String</span>)
  <span class="hljs-keyword">case</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Entry</span>(</span>key: <span class="hljs-type">String</span>, value: <span class="hljs-type">Long</span>)
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StorageException</span>(</span>msg: <span class="hljs-type">String</span>) <span class="hljs-keyword">extends</span> <span class="hljs-type">RuntimeException</span>(msg)
}

<span class="hljs-comment">/**
 * Saves key/value pairs to persistent storage when receiving `Store` message.
 * Replies with current value when receiving `Get` message.
 * Will throw StorageException if the underlying data store is out of order.
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Storage</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Actor</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">Storage</span>._

  <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">db</span> =</span> <span class="hljs-type">DummyDB</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">receive</span> =</span> <span class="hljs-type">LoggingReceive</span> {
    <span class="hljs-keyword">case</span> <span class="hljs-type">Store</span>(<span class="hljs-type">Entry</span>(key, count)) =&gt; db.save(key, count)
    <span class="hljs-keyword">case</span> <span class="hljs-type">Get</span>(key)                 =&gt; sender() ! <span class="hljs-type">Entry</span>(key, db.load(key).getOrElse(<span class="hljs-number">0</span>L))
  }
}

<span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">DummyDB</span> {</span>
  <span class="hljs-keyword">import</span> <span class="hljs-type">Storage</span>.<span class="hljs-type">StorageException</span>
  <span class="hljs-keyword">private</span> <span class="hljs-keyword">var</span> db = <span class="hljs-type">Map</span>[<span class="hljs-type">String</span>, <span class="hljs-type">Long</span>]()

  <span class="hljs-annotation">@throws</span>(classOf[<span class="hljs-type">StorageException</span>])
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">save</span>(</span>key: <span class="hljs-type">String</span>, value: <span class="hljs-type">Long</span>): <span class="hljs-type">Unit</span> = synchronized {
    <span class="hljs-keyword">if</span> (<span class="hljs-number">11</span> &lt;= value &amp;&amp; value &lt;= <span class="hljs-number">14</span>)
      <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-type">StorageException</span>(<span class="hljs-string">"Simulated store failure "</span> + value)
    db += (key -&gt; value)
  }

  <span class="hljs-annotation">@throws</span>(classOf[<span class="hljs-type">StorageException</span>])
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">load</span>(</span>key: <span class="hljs-type">String</span>): <span class="hljs-type">Option</span>[<span class="hljs-type">Long</span>] = synchronized {
    db.get(key)
  }
}
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3/03_fault_tolerance.html" class="navigation navigation-prev " aria-label="Previous page: 容错"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/04_dispatchers.html" class="navigation navigation-next " aria-label="Next page: 调度器"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
